package servlet.user;

import static database.Config.properties;

import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ShowImage.
 * On recupere l'image garde dans la base de donnes et on l'affiche sur l'ecran. 
 * Si l'utilisateur n'a pas d'image, on lui donne un image par defaut.
 */
public class ShowImage extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowImage() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String user = (String)request.getSession().getAttribute("id");
		String connectionURL = properties.getProperty("dbURL");
		java.sql.Connection con=null;
		boolean noPhoto=false;
		try{
			Class.forName(properties.getProperty("driver")).newInstance();
			con=DriverManager.getConnection(connectionURL,properties.getProperty("username"),properties.getProperty("password"));
			Statement st1=con.createStatement();
			ResultSet rs1 = st1.executeQuery("select photo from User where user='"+user+"'");
			String imgLen="";
				if(rs1.next()){			
					imgLen = rs1.getString(1);
					if(imgLen == null)
						noPhoto=true;
				}
				
			if(!noPhoto) {
				
				rs1 = st1.executeQuery("select photo from User where user='"+user+"'");
					if(rs1.next()){
						int len = imgLen.length();
						byte [] rb = new byte[len];
						InputStream readImg = rs1.getBinaryStream(1);
						int index=readImg.read(rb, 0, len);
						System.out.println("index"+index);
						st1.close();
						response.reset();
						response.setContentType("image/jpg");
						response.getOutputStream().write(rb,0,len);
						response.getOutputStream().flush();
					}
				}
			//}
			else{
				ResultSet rs2 = st1.executeQuery("select photo from User where user='default'");
				String imgLen2="";
				if(rs2!=null) {
					if(rs2.next()){
						imgLen2 = rs2.getString(1);
						System.out.println(imgLen2.length());
					}
					rs2 = st1.executeQuery("select photo from User where user='default'");
					if(rs2.next()){
						int len = imgLen2.length();
						byte [] rb = new byte[len];
						InputStream readImg = rs2.getBinaryStream(1);
						int index=readImg.read(rb, 0, len);
						System.out.println("index"+index);
						st1.close();
						response.reset();
						response.setContentType("image/jpg");
						response.getOutputStream().write(rb,0,len);
						response.getOutputStream().flush();
					}
				}
			}
		} catch (Exception e){
			e.printStackTrace();
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated methood stub
		doGet(request,response);
	}

}
